import java.awt.Color;
import java.awt.Graphics;
import java.lang.Runnable;
import java.rmi.RemoteException;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class MandelbrotRenderer implements Runnable {

	private int width, height, section, offset;
	private Vetor2D position;
	private double zoom;
	private Graphics g;
	private MandelbrotServer server;

	public MandelbrotRenderer(int width, int height, int section, int offset, 
							Vetor2D position, double zoom, Graphics g, MandelbrotServer server) {
		this.width = width;
		this.height = height;
		this.section = section;
		this.offset = offset;
		this.position = position;
		this.zoom = zoom;
		this.g = g;
		this.server = server;
	}

	public void run() {

		Image img = new BufferedImage(width, offset, BufferedImage.TYPE_INT_RGB);
		Graphics i = img.getGraphics();

		try {
			int part[] = 
				server.renderRegion(width, height, section, offset, 
				position.x, position.y, zoom);

			for (int x = 0; x < width; x++)
				for (int y = 0; y < offset; y++) {
						int n = part[(width*y)+x];
						i.setColor((n == MandelbrotServerImpl.ITERACOES) ? Color.BLACK :
							new Color((int) ((Math.cos(n / 10.0f) + 1.0f) * 127.0f),
							(int) ((Math.cos(n / 20.0f) + 1.0f) * 127.0f),
							(int) ((Math.cos(n / 300.0f) + 1.0f) * 127.0f)));
						i.drawLine(x, y, x+1, y);
					}
		} catch (RemoteException ex) {			
			i.setColor(Color.WHITE);
			i.drawString(ex.toString(), 1, 12);
		}
		g.drawImage(img, 0, section, null); 
	}

}
